<?php
/**
 * Created by PhpStorm.
 * User: 17208
 * Date: 2019/6/5
 * Time: 17:31
 */

namespace app\api\controller;
use app\common\lib\Request;
use app\api\model\Message_prompt;
use \think\Exception;
use app\common\lib\Export;
use app\api\model\Team_team;
use app\api\model\Team_user;
use app\api\task\Prompt;
use think\Container;

class Team extends Base
{
    private $cmd_data = [
        /** 新建团队 */
        "100" => "save_team",
        /** 加入团队 */
        "101" => "join_team",
        /** 退出团队 */
        "102" => "exit_team",
    ];

    public function index()
    {
        try{
            // 检验登录情况
            parent::check_user_info();

            $cmd = Request::get('cmd');
            $method = $this->cmd_data[$cmd];
            $this->$method();
        }catch (Exception $e){
            Export::exp_err($e->getMessage(),$e->getCode());
        }
        return '';
    }
    /** 100 保存团队 */
    private function save_team(){
        $data = Request::post(["id","name","desc","pic"]);
        if($data['id']){
            $team = Team_team::get($data['id']);
            if(!$team){
                throw new Exception(Team_team::$object_name."不存在");
            }
            $team->name = $data['name'];
            $team->desc = $data['desc'];
            $team->pic = $data['pic'];
            $team->save();
        }else{
            $team = Team_team::create([
                "name"=>$data['name'],
                "desc"=>$data['desc'],
                "pic"=>$data['pic'],
                "master"=>$this->user_id,
            ]);
        }

        Team_user::create([
            "team_id"=>$team['id'],
            "user_id"=>$this->user_id,
            "user_alias"=>$this->user_name,
        ]);
        Export::exp_ok();
    }
    /** 申请加入团队 */
    private function join_team(){
        $data = Request::post(["team_id"]);
        $team = Team_team::get($data['team_id']);
        if(!$team) throw new Exception(Team_team::$object_name."不存在");

        $team_user = Team_user::where([
            "team_id"=>$data['team_id'],
            "user_id"=>$this->user_id,
            "user_alias"=>$this->user_name,
        ])->find();
        if($team_user) throw new Exception("你已加入".$team['name']);

        $team_user = Team_user::create([
           "team_id"=>$data['team_id'],
            "user_id"=>$this->user_id,
            "user_alias"=>$this->user_name,
        ]);
        $this->save_prompt($team->master,$this->user_id,$team->id,$team_user->id,Message_prompt::$join_team_type);

        Export::exp_ok();
    }
    /** 退出团队 */
    private function exit_team(){
        $data = Request::post(["team_id"]);
        $team = Team_team::get($data['team_id']);
        if(!$team) throw new Exception(Team_team::$object_name."不存在");

        $team_user = Team_user::where([
            "team_id"=>$data['team_id'],
            "user_id"=>$this->user_id,
        ])->find();
        if(!$team_user) throw new \Exception("你未加入".Team_team::$object_name);

        $team_user->delete();

        $this->save_prompt($team_user->master,$this->user_id,$team->id,$team_user->id,Message_prompt::$exit_team_type);

        Export::exp_ok();
    }
    /**
     * 异步插入消息提示
     * @param int type 加入消息通知
     */
    private function save_prompt($user_id,$operate_user_id,$object_id,$operate_id,$type,$increment=1){

        $task_data = [
            "user_id"=>$user_id,
            "operate_user_id"=>$operate_user_id,
            "operate_id"=>$operate_id,
            "object_id"=>$object_id,
            "type"=>$type,
            "increment"=>$increment,
        ];

        $task_Dynamic = new Prompt(100,$task_data);
        Container::get('swoole')->task($task_Dynamic);
    }

}